# coding:utf-8
from ThreadProxy import *
import ConfigParser
import geoip2.database,os
import inspect


#公用
c=inspect.currentframe()
namefilepath = os.path.dirname(c.f_code.co_filename)
#配置文件
config = ConfigParser.ConfigParser()
config.read( './main.ini')

#所有队列
queue = Queue.Queue() #待验证的地址
Vaildqueue = Queue.Queue()  #验证以后的活的地址
NotVaildqueue = Queue.Queue() #验证以后的死的地址
Vialdqueue_ = Queue.Queue() #验证以后活的地址的时间设置内的重复验证
cache = redis.StrictRedis(host=config.get('redis','host'),port=config.getint('redis','port'),db=config.getint('redis','db')) #设置缓存
reader = geoip2.database.Reader('./geoip/GeoLite2-City.mmdb')
class main():
    thread_dict = {'proxy_thread':[],'proxy_thread_':[],'vaild_proxy_thread':None,'notvaild_proxy_thread':None,'job_thread':None,'job_thread_':None}
    def __init__(self):
        cache.set('thread_count',config.getint('thread','thread_count'))
        cache.set('vaild_thread_count',config.getint('thread','vaild_thread_count'))
        cache.set('proxy_thread',config.getint('thread','proxy_thread'))
        cache.set('vaild_proxy_thread',config.getint('thread','vaild_proxy_thread'))
        cache.set('notvaild_proxy_thread',config.getint('thread','notvaild_proxy_thread'))
        cache.set('proxy_thread_',config.getint('thread','proxy_thread_'))
        cache.set('job_thread',config.getint('thread','job_thread'))
        cache.set('job_thread_',config.getint('thread','job_thread_'))
        cache.set('proxy_time',config.getint('thread','proxy_time'))
        cache.set('proxy_vaild_time',config.getint('thread','proxy_vaild_time'))
        cache.set('proxy_url',config.get('thread','proxy_url'))
        cache.set('proxy_vaild_again',config.getint('thread','proxy_vaild_again'))
        cache.set('proxy_time_ms_out',config.getint('thread','proxy_time_ms_out'))

    def proxy_thread_func(self):
        #global thread_count,queue,Vaildqueue,NotVaildqueue
        '''
        #代理ip验证的线程
        '''
        if len(self.thread_dict['proxy_thread']) == 0 :#判断线程是否启动
            for i in range(int(cache.get('thread_count'))):
                t = ThreadUrl(queue,Vaildqueue,NotVaildqueue,cache,reader)
                t.setDaemon(True)
                t.start()
                self.thread_dict['proxy_thread'].append(t)
            print 'proxy_thread_func is start ...'
            echo( 'startting' )
    def proxy_thread_func_stop(self):
        for i in self.thread_dict['proxy_thread']:
            i.stop()
        self.thread_dict['proxy_thread'] = [] #清空线程列表
        print 'proxy_thread_func_stop is stop ... \n' #控制台信息输出
        echo( 'stop' ) #日志信息输出
    def vaild_proxy_thread_fuc(self):
        #global Vaildqueue,cache
        '''
        #处理活着的代理ip的数据操作的线程
        '''
        if self.thread_dict['vaild_proxy_thread'] is None:
            self.thread_dict['vaild_proxy_thread'] = ThreadVaild(Vaildqueue,cache)
            self.thread_dict['vaild_proxy_thread'].setDaemon(True)
            self.thread_dict['vaild_proxy_thread'].start()
            print 'vaild_proxy_thread_fuc is start ...'
            echo( 'startting' )
    def vaild_proxy_thread_fuc_stop(self):
        if self.thread_dict['vaild_proxy_thread'] is not None:
            self.thread_dict['vaild_proxy_thread'].stop()
        self.thread_dict['vaild_proxy_thread'] = None
        print 'vaild_proxy_thread_fuc_stop is stop ... \n'
        echo( 'stop' )
    def notviaild_proxy_thread_func(self):
        #global NotVaildqueue,cache
        '''
        #处理死了的代理ip的数据操作线程
        '''
        if self.thread_dict['notvaild_proxy_thread'] is None:
            self.thread_dict['notvaild_proxy_thread'] = ThreadNotVaild(NotVaildqueue,cache)
            self.thread_dict['notvaild_proxy_thread'].setDaemon(True)
            self.thread_dict['notvaild_proxy_thread'].start()
            print 'notviaild_proxy_thread_func is start ...\n'
            echo( 'startting' )
    def notviaild_proxy_thread_func_stop(self):
        if self.thread_dict['notvaild_proxy_thread'] is not None:
            self.thread_dict['notvaild_proxy_thread'].stop()
        self.thread_dict['notvaild_proxy_thread'] = None
        print 'notviaild_proxy_thread_func_stop is stop ... \n'
        echo( 'stop' )
    def proxy_thread_func_(self):
        #global vaild_thread_count,Vialdqueue_,NotVaildqueue
        '''
        #检查活着的代理ip是否有死掉的，有的话，直接扔进垃圾站
        '''
        if len(self.thread_dict['proxy_thread_']) ==0:
            for o in range(int(cache.get('vaild_thread_count'))):
                p = ThreadUrl_(Vialdqueue_,NotVaildqueue,cache,reader)
                p.setDaemon(True)
                p.start()
                self.thread_dict['proxy_thread_'].append(p)
            print 'proxy_thread_func_ is start ...\n'
            echo( 'startting' )
    def proxy_thread_func_stop_(self):
        for i in self.thread_dict['proxy_thread_']:
            i.stop()
        self.thread_dict['proxy_thread_'] = []
        print 'proxy_thread_func_stop_ is stop ... \n'
        echo( 'stop' )
    def job_thread_func(self):
        #global queue,cache
        '''
        #任务使用线程进行启动。
        '''
        if self.thread_dict['job_thread'] is None:
            self.thread_dict['job_thread'] = JobTreadh(queue,cache)
            self.thread_dict['job_thread'].setName('doing')
            self.thread_dict['job_thread'].start()
            print 'job_thread_func is start ...\n'
            echo( 'startting' )
    def job_thread_func_stop(self):
        if self.thread_dict['job_thread'] is not None:
            self.thread_dict['job_thread'].stop()
        self.thread_dict['job_thread'] = None
        print 'job_thread_func_stop is stop ... \n'
        echo( 'stop' )
    def job_thread_func_(self):
        #global queue,cache
        '''
        #活代理任务使用线程进行启动。
        '''
        #if self.thread_dict['job_thread_'] is None: #此句目前有bug
        self.thread_dict['job_thread_'] = JobTreadh_(Vialdqueue_,cache)
        self.thread_dict['job_thread_'].setName('doing_')
        self.thread_dict['job_thread_'].start()
        print 'job_thread_func_ is start ...\n'
        echo( 'startting' )
    def job_thread_func_stop_(self):
        if self.thread_dict['job_thread_'] is not None:
            self.thread_dict['job_thread_'].stop()
        self.thread_dict['job_thread_'] = None
        print 'job_thread_func_stop_ is stop ... \n'
        echo( 'stop' )
    def join(self):
        #global queue,Vaildqueue,NotVaildqueue,Vialdqueue_
        '''
        #开启所有的队列阻塞
        '''
        print 'the server is startting...\n'
        echo( 'the server is startting...' )
        queue.join()
        Vaildqueue.join()
        NotVaildqueue.join()
        Vialdqueue_.join()

    def proxy_thread_func_timer(self):
        '''
        通过1秒钟的时间在线检测redis当中的参数来修改当前的状态 此为proxy_thread_func方法的开关
        '''
        proxy_thread = cache.get('proxy_thread')
        if proxy_thread == '1':
            self.proxy_thread_func()
            #cache.set('proxy_thread',-1)
        elif proxy_thread == '0':
            self.proxy_thread_func_stop()
            #cache.set('proxy_thread',-1)
        threading.Timer(1.0,self.proxy_thread_func_timer).start()

    def vaild_proxy_thread_fuc_timer(self):
        '''
        通过1秒钟的时间在线检测redis当中的参数来修改当前的状态 此为vaild_proxy_thread_fuc方法的开关
        '''
        vaild_proxy_thread = cache.get('vaild_proxy_thread')
        if vaild_proxy_thread == '1':
            self.vaild_proxy_thread_fuc()
            #cache.set('vaild_proxy_thread',-1)
        elif vaild_proxy_thread == '0':
            self.vaild_proxy_thread_fuc_stop()
            #cache.set('vaild_proxy_thread',-1)
        threading.Timer(1.0,self.vaild_proxy_thread_fuc_timer).start()

    def notviaild_proxy_thread_func_timer(self):
        '''
        通过1秒钟的时间在线检测redis当中的参数来修改当前的状态 此为notviaild_proxy_thread_func方法的开关
        '''
        notvaild_proxy_thread = cache.get('notvaild_proxy_thread')
        if notvaild_proxy_thread == '1':
            self.notviaild_proxy_thread_func()
            #cache.set('notvaild_proxy_thread',-1)
        elif notvaild_proxy_thread == '0':
            self.notviaild_proxy_thread_func_stop()
            #cache.set('notvaild_proxy_thread',-1)
        threading.Timer(1.0,self.notviaild_proxy_thread_func_timer).start()

    def proxy_thread_func_timer_(self):
        '''
        通过1秒钟的时间在线检测redis当中的参数来修改当前的状态 此为proxy_thread_func_方法的开关
        '''
        proxy_thread_ = cache.get('proxy_thread_')
        if proxy_thread_ == '1':
            self.proxy_thread_func_()
            #cache.set('proxy_thread_',-1)
        elif proxy_thread_ == '0':
            self.proxy_thread_func_stop_()
            #cache.set('proxy_thread_',-1)
        threading.Timer(1.0,self.proxy_thread_func_timer_).start()

    def job_thread_func_timer(self):
        '''
        通过1秒钟的时间在线检测redis当中的参数来修改当前的状态 此为job_thread_func方法的开关
        '''
        job_thread = cache.get('job_thread')
        if job_thread == '1':
            self.job_thread_func()
            #cache.set('job_thread',-1)
        elif job_thread == '0':
            self.job_thread_func_stop()
            #cache.set('job_thread',-1)
        threading.Timer(1.0,self.job_thread_func_timer).start()

    def job_thread_func_timer_(self):
        '''
        通过1秒钟的时间在线检测redis当中的参数来修改当前的状态 此为job_thread_func方法的开关
        '''
        job_thread_ = cache.get('job_thread_')
        proxy_vaild_time = float(cache.get('proxy_vaild_time'))
        if job_thread_ == '1':
            self.job_thread_func_()
            #cache.set('job_thread_',-1)
        elif job_thread_ == '0':
            self.job_thread_func_stop_()
            #cache.set('job_thread_',-1)
        threading.Timer(proxy_vaild_time,self.job_thread_func_timer_).start()

    def run(self):
        '''
        self.proxy_thread_func()
        self.vaild_proxy_thread_fuc()
        self.notviaild_proxy_thread_func()
        self.proxy_thread_func_()
        self.job_thread_func()
        self.join()
        '''
        threading.Timer(1.0,self.proxy_thread_func_timer).start()
        threading.Timer(1.0,self.vaild_proxy_thread_fuc_timer).start()
        threading.Timer(1.0,self.notviaild_proxy_thread_func_timer).start()
        threading.Timer(1.0,self.proxy_thread_func_timer_).start()
        threading.Timer(1.0,self.job_thread_func_timer).start()
        threading.Timer(1.0,self.job_thread_func_timer_).start()
        threading.Timer(3.0,self.join).start()

if __name__ == '__main__':
    test = main()
    test.run()

'''
cache.sadd('proxy_vaild',['111','222'])
cache.sadd('proxy_vaild',['111','222','222222'])
cache.srem('proxy_vaild',['1111'])

cache.sismember('proxy_vaild',['1111']) is True
a= cache.smembers('proxy_vaild')

#print a.pop()
#print a.pop()

#print a
#print cache.scard('proxy_vaild')
a= cache.smembers('proxy_vaild')

#print a.pop()
#print a.pop()

cache.hset('qq','127.0.0.1:8080@HTTP','127.0.0.1:8080@HTTP@1@2')
cache.hset('qq','127.0.0.2:8080@HTTP','127.0.0.2:8080@HTTP@1@2')

cache.hdel('qq','127.0.0.2:8080@HTTP')
cache.hset('qq','127.0.0.3:8080@HTTP','127.0.0.3:8080@HTTP@1@2')
print cache.hvals('qq')
print cache.hexists('qq','127.0.0.3:8080@HTTP')
print cache.hget('qq','127.0.0.3:8080@HTTP')
print cache.hgetall('qq')
print cache.hkeys('qq')
print cache.hlen('qq')
print cache.hsetnx('qq','127.0.0.3:8080@HTTP','127.0.0.3:8080@HTTP@1@2')
print cache.hsetnx('qq','127.0.0.3:8080@HTTP',1)
print cache.hsetnx('qq','127.0.0.3:8080@HTTP',2)
print cache.hmget('qq',['127.0.0.1:8080@HTTP','127.0.0.2:8080@HTTP'])
cache.hmset('qq',{'1':'2','3':'5'})
print cache.hgetall('qq')



'''